﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKetNoi;
using QLDVu.BO;

namespace QLDVu.GUI
{
    public partial class SuaChuaForm : Form
    {
        string MaBaoGia = "";

        bool DaTonTaiPhieuSua = false;

        public SuaChuaForm()
        {
            InitializeComponent();

            cbTinhTrang.SelectedIndex = 0;
        }


        public SuaChuaForm(string MaBaoGia)
        {
            InitializeComponent();

            cbTinhTrang.SelectedIndex = 0;

            this.MaBaoGia = MaBaoGia;

            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            if (SuaChuaBO.LayThongTinPhieuSuaTheoMaBaoGia(MaBaoGia) != null)
            {
                DaTonTaiPhieuSua = true;
                btnLuu.Enabled = false;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
                EnableControl(false);
            }
            else
            {
                DaTonTaiPhieuSua = false;
                btnSua.Enabled = false;
                btnLuu.Enabled = true;
                btnXoa.Enabled = false;
                EnableControl(true);
            }
        }



        private void SuaChuaForm_Load(object sender, EventArgs e)
        {
            //Load thong tin Bao Gia
            Dictionary<string, string> dic = new Dictionary<string, string>();
            List<string> list = new List<string>();
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
            SuaChuaBO.LayThongTinBaoGiaSuaChua(MaBaoGia, out dic, out list);
            txtMaPhieuSua.Text = SuaChuaBO.SinhMaSuaChua();
            txtSoBaoGia.Text = dic["SoBaoGia"];
            txtNgayLap.Text = dic["NgayLap"];
            txtDienThoai.Text = dic["DienThoai"];
            txtDiaChi.Text = dic["DiaChi"];
            txtEmail.Text = dic["Email"];
            txtBienSo.Text = dic["BienSo"];
            txtTenKhach.Text = dic["TenKhach"];


            //tao DataGridViewCombobox TenNhanVien
            var comboNV = new DataGridViewComboBoxColumn() { Name = "TenNhanVien" };
            comboNV.HeaderText = "Nhân viên thực hiện";
            comboNV.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            comboNV.DataSource = SuaChuaBO.LayDanhSachNhanVien();
            dgvChiTietSuaChua.Columns.Insert(2, comboNV);

            //Load thong tin dich vu
            if (!DaTonTaiPhieuSua)
            {
                btnLuu.Text = "Tạo mới";
                List<string> arrayMaChiTietSua = SuaChuaBO.SinhMaChiTietSuaChua(list.Count);
                int index = 0;
                foreach (string item in list)
                {
                    string[] row = new string[] { arrayMaChiTietSua[index], item };
                    index++;
                    dgvChiTietSuaChua.Rows.Add(row);
                }
            }


            //Load thong tin PhieuSua (neu co)
            if (DaTonTaiPhieuSua)
            {
                btnLuu.Text = "Lưu";
                SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaBaoGia(txtSoBaoGia.Text);
                txtMaPhieuSua.Text = SuaChua.MaSuaChua;
                dtpNgayVao.Value = SuaChua.NgayVao;
                dtpNgayHoanThanh.Value = (DateTime)SuaChua.NgayHoanThanh;
                txtKmVao.Text = SuaChua.SoKmVao.ToString();
                txtKmRa.Text = SuaChua.SoKmRa.ToString();
                txtGhiChu.Text = SuaChua.GhiChu;
                cbTinhTrang.Text = SuaChua.TinhTrang;

                //Load thong tin chi tiet phieu sua chua
                List<string[]> ThongTinCTSC = new List<string[]>();
                ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();
                ThongTinCTSC = ChiTietSuaBO.LayThongTinChiTietSuaChua(txtMaPhieuSua.Text);
                foreach (var s in ThongTinCTSC)
                {
                    dgvChiTietSuaChua.Rows.Add(s);
                }
            }
        }



        private void btnLuu_Click(object sender, EventArgs e)
        {
           
            PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();

            if (!DaTonTaiPhieuSua)    //Them moi phieu sua
            {
                SUACHUA SuaChua = new SUACHUA();

                //SuaChua.MaSuaChua = SuaChuaBO.SinhMaSuaChua();
                SuaChua.MaSuaChua = txtMaPhieuSua.Text;
                SuaChua.MaBaoGia = txtSoBaoGia.Text;
                SuaChua.NgayVao = dtpNgayVao.Value;
                SuaChua.NgayHoanThanh = dtpNgayHoanThanh.Value;
                SuaChua.GhiChu = txtGhiChu.Text;
                SuaChua.TinhTrang = cbTinhTrang.SelectedItem.ToString();
                SuaChua.ConLai = SuaChuaBO.LayTongTien(txtSoBaoGia.Text).TongTien;

                if (txtKmVao.Text != "")
                    SuaChua.SoKmVao = Convert.ToInt32(txtKmVao.Text);
                if (txtKmRa.Text != "")
                    SuaChua.SoKmRa = Convert.ToInt32(txtKmRa.Text);

                SuaChuaBO.ThemPhieuSuaChua(SuaChua);

                LuuChiTietPhieuSua(); //Luu chi tiet phieu sua
                MessageBox.Show("Thêm phiếu sửa thành công");
                DaTonTaiPhieuSua = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;
            }
            else //Sua thong tin phieu sua
            {
                SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaBaoGia(txtSoBaoGia.Text);
                string MaPhieuSua = txtMaPhieuSua.Text;
                SuaChua.NgayHoanThanh = dtpNgayHoanThanh.Value;
                SuaChua.GhiChu = txtGhiChu.Text;
                SuaChua.TinhTrang = cbTinhTrang.Text;

                if (txtKmRa.Text != "" && txtKmVao.Text != "")
                    SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), Convert.ToInt32(txtKmVao.Text), dtpNgayHoanThanh.Value, Convert.ToInt32(txtKmRa.Text), txtGhiChu.Text);
                else if (txtKmVao.Text != "")
                    SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), Convert.ToInt32(txtKmVao.Text), dtpNgayHoanThanh.Value, txtGhiChu.Text);
                else if (txtKmRa.Text != "")
                    SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), dtpNgayHoanThanh.Value, Convert.ToInt32(txtKmRa.Text), txtGhiChu.Text);
                else
                    SuaChuaBO.SuaThongTinPhieuSua(MaPhieuSua, dtpNgayVao.Value, cbTinhTrang.SelectedItem.ToString(), dtpNgayHoanThanh.Value, txtGhiChu.Text);

                SuaChiTietPhieuSua(); //Sua thong tin chi tiet phieu sua
                MessageBox.Show("Đã cập nhật thông tin phiếu sữa");
                DaTonTaiPhieuSua = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;
            }

            this.Close();

        }


        private void EnableControl(bool value)
        {
            dtpNgayVao.Enabled = value;
            dtpNgayHoanThanh.Enabled = value;
            txtKmVao.Enabled = value;
            txtKmRa.Enabled = value;
            txtGhiChu.Enabled = value;
            dgvChiTietSuaChua.Enabled = value;
            
        }


        private void btnSua_Click(object sender, EventArgs e)
        {
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = true;
            btnLuu.Text = "Lưu";
            EnableControl(true);
        }


        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn muốn xóa ?", "Xác nhận", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                //Xoa Chi tiet Phieu sua truoc
                XoaChiTietPhieuSua(txtMaPhieuSua.Text);

                //Sau do xoa Phieu sua
                PhieuSuaChuaBO SuaChuaBO = new PhieuSuaChuaBO();
                SUACHUA SuaChua = SuaChuaBO.LayThongTinPhieuSuaTheoMaPhieuSua(txtMaPhieuSua.Text);
                SuaChuaBO.XoaPhieuSuaChua(SuaChua.MaSuaChua);
                MessageBox.Show("Xóa thành công");
            }
            else
            {
                return;
            }
            this.Close();
        }
        private void XoaChiTietPhieuSua(string MaPhieuSua)
        {
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();

            ChiTietSuaBO.XoaChiTietPhieuSuaTuMaPhieuSua(MaPhieuSua);
        }
        private void dgvChiTietSuaChua_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            }
        }


        private void LayDanhSachCTPhieuSua()
        {

        }


        private void LuuChiTietPhieuSua()
        {
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();
            for (int i = 0; i < dgvChiTietSuaChua.RowCount; i++)
            {
                CHITIETPHIEUSUACHUA ctps = new CHITIETPHIEUSUACHUA();
                ctps.MaChiTietSuaChua = dgvChiTietSuaChua.Rows[i].Cells[0].Value.ToString();
                ctps.MaSuaChua = txtMaPhieuSua.Text;
                string TenDV = dgvChiTietSuaChua.Rows[i].Cells[1].Value.ToString();
                ctps.MaDichVu = ChiTietSuaBO.LayMaDichVuTuTenDV(TenDV);
                string TenND = "";
                if (dgvChiTietSuaChua.Rows[i].Cells[2].Value != null)
                {
                    TenND = dgvChiTietSuaChua.Rows[i].Cells[2].Value.ToString();
                    ctps.MaNguoiDung = ChiTietSuaBO.LayMaNguoiDungTuTenND(TenND);
                }
                ChiTietSuaBO.ThemChiTietPhieuSua(ctps);
            }   
            
        }



        private void SuaChiTietPhieuSua()
        {
            ChiTietSuaChuaBO ChiTietSuaBO = new ChiTietSuaChuaBO();
            for (int i = 0; i < dgvChiTietSuaChua.RowCount; i++)
            {
                string TenNguoiDung = null;
                string MaChiTietSC = dgvChiTietSuaChua.Rows[i].Cells[0].Value.ToString();
                if (dgvChiTietSuaChua.Rows[i].Cells[2].Value != null)
                    TenNguoiDung = dgvChiTietSuaChua.Rows[i].Cells[2].Value.ToString();
                ChiTietSuaBO.SuaChiTietPhieuSua(MaChiTietSC, TenNguoiDung);
            }
        }



    }
}
